package com.zjsru.plan2024.oneday;

import java.util.Arrays;

/**
 * 2575. 找出字符串的可整除数组
 * @Author: cookLee
 * @Date: 2024-03-07
 */
public class DivisibilityArray {

    /**
     * 主
     * \
     * 输入：word = "998244353", m = 3
     * 输出：[1,1,0,0,0,1,1,0,0]
     * 解释：仅有 4 个前缀可以被 3 整除："9"、"99"、"998244" 和 "9982443" 。
     * \
     * @param args args
     */
    public static void main(String[] args) {
        String word = "998244353";
        int m = 3;
        DivisibilityArray divisibilityArray = new DivisibilityArray();
        System.out.println(Arrays.toString(divisibilityArray.divisibilityArray(word, m)));
    }

    /**
     * 除数数组
     *
     * @param word 字
     * @param m    m
     * @return {@link int[]}
     */
    public int[] divisibilityArray(String word, int m) {
        int[] ans = new int[word.length()];
        //定义一个余数，使用long防止数组越界
        long cur = 0;
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            //获取上一位除后的余数 与当前数进行拼接 再次取模
            cur = (cur * 10 + (c - '0')) % m;
            //如果是0则刚好可以整除，放入1
            ans[i] = (cur == 0) ? 1 : 0;
        }
        return ans;
    }

}
